#include <stdio.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "errsys.h"

int main(int argc, char **argv)
{
	if(argc != 3)
	{
		fprintf(stderr,"%s file1 file2\n",argv[0]);
		exit(1);
	}

	struct pollfd fds[2];
	char buf[4096];

	fds[0].fd = open(argv[1],O_RDONLY);
	if(fds[0].fd==-1)
		err_sys("open");

	fds[1].fd = open(argv[2],O_RDONLY);
	if(fds[1].fd==-1)
		err_sys("open");

	fds[0].events = POLLIN;
	fds[1].events = POLLIN;

	while(fds[0].events || fds[1].events)
	{
		if((poll(fds,2,0))<0)
			err_sys("poll");

		int i=0;
		for(; i<2;++i)
		{
			if(fds[i].revents)
			{
				int n;
				n=read(fds[i].fd,buf,sizeof(buf)-1);
				if(n<0)
					err_sys("read");
				else if(n==0)
					fds[i].events = 0;
				else {
					buf[n]='\0';
					fprintf(stdout,"%s\n",buf);
					fflush(stdout);
				}
			}
		}
	}

	return 0;
}

